package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Client {

	static final String serverIpAddress = "172.23.92.225";
	
	static final int serverPort = 3333; 
	
	static final String joinCmd = "startGame";
	static final String moveCmd = "move";
	static final String quitCmd = "quit";
	static final boolean isServer = true;
	
	public static final String OKCode = "200";
	
	String username = "";
	boolean inGame = false;
	int collectedTreasure = 0, x = 0,y = 0;
	
	public String readCommand(){
		String msg = "";
		
		BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
		try {
			msg = userInput.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return msg;
	}
	
	public void printMessage(String message){
		System.out.println(message);
	}
	
	public void requestGame(){		
		if (!ClientConnection.openConnection(serverIpAddress,serverPort))
		{
			printMessage("Cannot connect to Server!");
			return;
		}		
		
		if (!ClientConnection.send(joinCmd))
		{
			printMessage("Cannot send Join-Game request to the Server!");
		}	
	}
	
	public String move(String direction){
		if (!ClientConnection.openConnection(serverIpAddress,serverPort))
			printMessage("Cannot connect to Server!");
		
		String moveMsg = moveCmd+"_"+username+"_"+direction.substring(4);
		
		if (!ClientConnection.send(moveMsg))
			printMessage("Cannot send Move request to the Server!");
		
		//waiting for the reply from server
		String reply = ClientConnection.receive();
	
		return reply;
	}

	public String quit(){
		if (!ClientConnection.openConnection(serverIpAddress,serverPort))
			printMessage("Cannot connect to Server!");
		
		String quitMsg = quitCmd+"_"+username;
		
		if (!ClientConnection.send(quitMsg))
			printMessage("Cannot send Quit request to the Server!");
		
		//waiting for the reply from server
		String reply = ClientConnection.receive();
	
		return reply;
	}
	
	public void run()
	{
		printMessage("Welcome to the Maze Game!\nTo join a game type 'join'\nTo exit a game type 'quit'\n");
		printMessage("To move around type 'Move' + direction of N,S,W,E,NM\n");
		String command = "";
		do {
			System.out.print(">");
			command = readCommand();
			if (command.equalsIgnoreCase("Join"))
			{
				requestGame();
				
				//client now waits for the reply from Server
				String reply = ClientConnection.receive();
				
				if (reply != null && reply.startsWith("200"))
				{
					StringTokenizer st = new StringTokenizer(reply,"_");
					
					//ok code
					if (st.hasMoreElements())
						st.nextElement();
					
					//userID
					if (st.hasMoreElements())
						username = st.nextToken();
					
					//x-pos
					if (st.hasMoreElements())
						x = Integer.parseInt(st.nextToken());
					
					//y-pos
					if (st.hasMoreElements())
						y = Integer.parseInt(st.nextToken());
					
					//collected treasures
					if (st.hasMoreElements())
						collectedTreasure = Integer.parseInt(st.nextToken());
					
										
					printMessage("You have collected : "+collectedTreasure+" treasures");
					printMessage("Your position ("+x+","+y+")\n");
					
					//game state
					while (st.hasMoreElements())
					{
						printMessage(st.nextToken());	
						//System.out.println("game state = "+st.nextToken());
					}
				}			
				else
					printMessage(reply);
				
			}
			else if ((command.equalsIgnoreCase("MoveN")) || (command.equalsIgnoreCase("MoveW")) || (command.equalsIgnoreCase("MoveE"))
					|| (command.equalsIgnoreCase("MoveS")) || (command.equalsIgnoreCase("MoveNM")))
			{
				String status = move(command);
				if (status != null && status.startsWith(OKCode))
				{
					StringTokenizer st = new StringTokenizer(status,"_");
					
					//ok code
					if (st.hasMoreElements())
						st.nextElement();
					
					//userID
					if (st.hasMoreElements())
					{
						if (!username.equalsIgnoreCase(st.nextToken()))
							printMessage("Wrong username!!!");
					}
					
					//x-pos
					if (st.hasMoreElements())
						x = Integer.parseInt(st.nextToken());
					
					//y-pos
					if (st.hasMoreElements())
						y = Integer.parseInt(st.nextToken());
					
					//collected treasures
					if (st.hasMoreElements())
						collectedTreasure = Integer.parseInt(st.nextToken());
					
										
					printMessage("You have collected : "+collectedTreasure+" treasures");
					printMessage("Your position ("+x+","+y+")\n");
					
					//game state
					while (st.hasMoreElements())
						printMessage(st.nextToken());	
				}
				else
					printMessage(status);		       	
			}
			else if (command.equalsIgnoreCase("Quit"))
			{
				String reply = quit();
				if (reply != null && reply.startsWith(OKCode))
				{
					StringTokenizer st = new StringTokenizer(reply,"_");
					
					//ok code
					if (st.hasMoreElements())
						st.nextElement();
					
					while (st.hasMoreElements())
						printMessage(st.nextToken());					
				}					
			}
		}
		while (!command.equalsIgnoreCase("Quit"));
		
		ClientConnection.closeConnection();
	}
	
	public static void main(String[] args) throws Exception
	{
		/*if (isServer)
		{
			Server server = new Server();
			server.run();
		}*/
		Client client = new Client();
		client.run();
	}
}
